package jtracking_server;
import jtracking_both.*;
import java.util.ArrayList;
import java.util.HashSet;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableRowSorter;

/*Das ist die Detailansicht der DetailGUI. Sie wird durch die UebersichtsGUI
 * aktiviert und gibt einen Ueberblick wann welche Einheit von wem wo verwendet wurd
 *(Siehe switch)*/

 //@author Felix
public class DetailGUI extends javax.swing.JFrame {

    Computer computer;
    DBTableModel dbModel;
    private TableRowSorter<DBTableModel> sorter;
    /** Creates new form DetailGUI */
    public DetailGUI(Computer computer) {
        super("JTracking Detail "+computer.getName());
        this.computer=computer;
        this.dbModel=new DBTableModel();
        sorter = new TableRowSorter<DBTableModel>();
        jTable1.setRowSorter( sorter );
        initComponents();
        //Öffnet DetailGUI (Detailansicht) mit dem Computer
    }
    DBManager dbm;



    class DBTableModel extends AbstractTableModel
    {
        public DBTableModel()
        {
            datenabrufen();
        }

        ArrayList<Datenpaket> daten;
        public void datenabrufen()
        {
            HashSet<Datenpaket> d =computer.getDatenpakete();
            daten= new ArrayList<Datenpaket>(d);
        }

        public int getRowCount() {
            //returnt Anzahl der Einträge
            return daten.size();
        }

        public int getColumnCount() {
            return 4;
        }
        public Object getValueAt(int rowIndex, int columnIndex) {

            switch (columnIndex) {
                    case 0: return daten.get(rowIndex).getDatum().toString();      //Nutzungsdatum
                    case 1: return daten.get(rowIndex).getRaum();       //Raum
                    case 2: return daten.get(rowIndex).getUsername();   //Benutzer
                    case 3: return null;//Nutzungsdauer
            }
            return null;       //Füllt Spalten mit Daten
        }
    
        @Override
        public String getColumnName(int column) {
            switch(column){
                case 0: return "Verwendungsdatum";
                case 1: return "Raum";
                case 2: return "Benutzer";
                case 3: return "Nutzungsdauer";

            }
            return "Datenbank nicht erreichbar"; //Falls nicht auslesbar
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        choice1 = new java.awt.Choice();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        label1 = new java.awt.Label();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jSeparator1 = new javax.swing.JSeparator();
        jMenu2 = new javax.swing.JMenu();

        choice1.setName("Fahrbare Einheit"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jTable1.setModel(dbModel);
        jScrollPane1.setViewportView(jTable1);

        label1.setText(computer.getName());

        jButton1.setText("Aktualisieren");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jMenuBar1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jMenu1.setText("Verwaltung/Wartung");
        jMenu1.add(jSeparator1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Hilfe/Kontakt");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(label1, javax.swing.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dbModel.datenabrufen();
        dbModel.fireTableDataChanged();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        Main.main.gui.detailWindowClosed();
    }//GEN-LAST:event_formWindowClosed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Choice choice1;
    private javax.swing.JButton jButton1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private java.awt.Label label1;
    // End of variables declaration//GEN-END:variables

}
